import os
import fire
from glob import glob

def single_get_first(unicode1):
    str1 = unicode1.encode('gbk')
    try:
        ord(str1)
        return str1
    except:
        asc = str1[0] * 256 + str1[1] - 65536
        if asc >= -20319 and asc <= -20284:
            return 'a'
        if asc >= -20283 and asc <= -19776:
            return 'b'
        if asc >= -19775 and asc <= -19219:
            return 'c'
        if asc >= -19218 and asc <= -18711:
            return 'd'
        if asc >= -18710 and asc <= -18527:
            return 'e'
        if asc >= -18526 and asc <= -18240:
            return 'f'
        if asc >= -18239 and asc <= -17923:
            return 'g'
        if asc >= -17922 and asc <= -17418:
            return 'h'
        if asc >= -17417 and asc <= -16475:
            return 'j'
        if asc >= -16474 and asc <= -16213:
            return 'k'
        if asc >= -16212 and asc <= -15641:
            return 'l'
        if asc >= -15640 and asc <= -15166:
            return 'm'
        if asc >= -15165 and asc <= -14923:
            return 'n'
        if asc >= -14922 and asc <= -14915:
            return 'o'
        if asc >= -14914 and asc <= -14631:
            return 'p'
        if asc >= -14630 and asc <= -14150:
            return 'q'
        if asc >= -14149 and asc <= -14091:
            return 'r'
        if asc >= -14090 and asc <= -13119:
            return 's'
        if asc >= -13118 and asc <= -12839:
            return 't'
        if asc >= -12838 and asc <= -12557:
            return 'w'
        if asc >= -12556 and asc <= -11848:
            return 'x'
        if asc >= -11847 and asc <= -11056:
            return 'y'
        if asc >= -11055 and asc <= -10247:
            return 'z'
        return ''


def getPinyin(string):
    if string == None:
        return None
    lst = list(string)
    charLst = []
    for l in lst:
        charLst.append(single_get_first(l))
    return ''.join(charLst)


def recalTrain(src, model_path='Recall/models'):
    common = 'python trainshell.py recallclf "{}"  {} --time="07:59" --monitor="http://localhost:8000/monitor/test_token" --isupdate=false \n'
    settings = '"{}":"{}",\n'
    # 迭代文件
    for filename in os.listdir(src):
        model_name = model_path + '/' + getPinyin(filename) + '.h5'
        fp = os.path.join(src, filename)
        common.format( fp , model_name )
        settings.format( filename , getPinyin(filename) )

    print( common )
    print('\n')
    print( settings )

def addUpImagesNum(src):
    daysList = glob(os.path.join(src, '*'))
    areaDetectNum = 0
    DocumentClassifierNum = 0
    LayoutClassifierNum = 0
    PreprocessingNum = 0
    RecalNum = 0
    AreaDetectClass  = {}
    DocumentClass = {}
    LayoutClass = {}
    RecallClass = {}
    for day in daysList:
        print(day)
        '''统计区域样本总数'''
        AreaDetectDocumentPath = os.path.join(day, 'AreaDetect\\datasets\\DocumentClassifier\\datasets')
        if os.path.exists(AreaDetectDocumentPath):
            AreaDetectDocumentList = os.listdir(AreaDetectDocumentPath)
            for classType in AreaDetectDocumentList:
                num = len(os.listdir(os.path.join(AreaDetectDocumentPath, classType)))
                if classType in AreaDetectClass.keys():
                    AreaDetectClass[classType] = AreaDetectClass[classType] + num
                else:
                    AreaDetectClass.setdefault(classType, num)
                areaDetectNum += num
        '''统计区域版面标记样本数'''
        AreaDetectLayoutPath = os.path.join(day, 'AreaDetect\\datasets\\LayoutClassifier\\datasets')
        if os.path.exists(AreaDetectLayoutPath):
            AreaDetectLayoutList = os.listdir(AreaDetectLayoutPath)
            for classType in AreaDetectLayoutList:
                num = len(os.listdir(os.path.join(AreaDetectLayoutPath, classType)))
                if classType in AreaDetectClass.keys():
                    AreaDetectClass[classType] = AreaDetectClass[classType] + num
                else:
                    AreaDetectClass.setdefault(classType, num)
                areaDetectNum += num
        '''统计证件类型样本数'''
        DocumentClassifierPath = os.path.join(day, 'DocumentClassifier\\datasets')
        if os.path.exists(DocumentClassifierPath):
            DocumentClassifierList = os.listdir(DocumentClassifierPath)
            for classType in DocumentClassifierList:
                num = len(os.listdir(os.path.join(DocumentClassifierPath, classType)))
                if classType in DocumentClass.keys():
                    DocumentClass[classType] = DocumentClass[classType] + num
                else:
                    DocumentClass.setdefault(classType, num)
                DocumentClassifierNum += num
        '''统计版面类型样本数'''
        LayoutClassifierPath = os.path.join(day, 'LayoutClassifier\\datasets')
        if os.path.exists(LayoutClassifierPath):
            LayoutClassifierList = os.listdir(LayoutClassifierPath)
            for classType in LayoutClassifierList:
                num = len(os.listdir(os.path.join(LayoutClassifierPath, classType)))
                if classType in LayoutClass.keys():
                    LayoutClass[classType] = LayoutClass[classType] + num
                else:
                    LayoutClass.setdefault(classType, num)
                LayoutClassifierNum += num
        '''统计纠偏样本数'''
        PreprocessingPath = os.path.join(day, 'Preprocessing\\datasets')
        if os.path.exists(PreprocessingPath):
            PreprocessingList = os.listdir(PreprocessingPath)

            PreprocessingNum += len(PreprocessingList)
        '''统计召回系统非证件类型样本数'''
        RecallPath = os.path.join(day, 'Recall\\datasets')
        if os.path.exists(RecallPath):
            RecallList = os.listdir(RecallPath)
            for classType in RecallList:
                num = len(os.listdir(os.path.join(RecallPath, classType, '0')))
                if classType in RecallClass.keys():
                    RecallClass[classType] = RecallClass[classType] + num
                else:
                    RecallClass.setdefault(classType, num)
                RecalNum += num
        # print('区域总样本数==》 {}\t证件类型总样本数==》{}\t版面类型总样本数==》{}\t纠偏类型总样本数==》{}\t召回系统总样本数==》{}'.format(
        #     areaDetectNum, DocumentClassifierNum, LayoutClassifierNum, PreprocessingNum, RecalNum
        # ))
    msg = '统计结果如下\n区域总样本数==》 {}\t证件类型总样本数==》{}\t版面类型总样本数==》{}\t纠偏类型总样本数==》{}\t召回系统总样本数==》{}'.format(
        areaDetectNum, DocumentClassifierNum, LayoutClassifierNum, PreprocessingNum, RecalNum
    )
    print(msg)
    print('\n')
    print('`````````````````区域类型样本详情如下`````````````````````')
    for key in AreaDetectClass:
        print('{}\t{}'.format(key, AreaDetectClass[key]))
    print('`````````````````证件类型样本详情如下`````````````````````')
    for key in DocumentClass:
        print('{}\t{}'.format(key, DocumentClass[key]))
    print('`````````````````版面类型样本详情如下`````````````````````')
    for key in LayoutClass:
        print('{}\t{}'.format(key, LayoutClass[key]))
    print('`````````````````召回系统样本详情如下`````````````````````')
    for key in RecallClass:
        print('{}\t{}'.format(key, RecallClass[key]))

def main(operation, src=None):
    if operation == 'recall':
        if src is None: src = 'Z:\\ai-训练数据\\训练\\Recall\\datasets'
        recalTrain(src)
    if operation == 'stat':
        if src is None: src = 'Z:\\ai-训练数据\\原图'
        addUpImagesNum(src)

if __name__ == '__main__':
    fire.Fire(main)
